import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solutions {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> lists=new ArrayList<>();
        List<Integer> list=new ArrayList<>();
        if(nums.length==0)
            return lists;
        boolean[] isVisited=new boolean[nums.length];
        dfs(nums,nums.length,0,isVisited,lists,list);
        return lists;
    }

    public void dfs(int[] nums,int len,int depth,boolean[] isVisited,List<List<Integer>> lists
    ,List<Integer> list){
        if(depth==len){
            lists.add(new ArrayList<>(list));
            return;
        }
        for (int i = 0; i < len; i++) {
            if(!isVisited[i]){
                list.add(nums[i]);
                isVisited[i]=true;
                dfs(nums,len,depth+1,isVisited,lists,list);
                isVisited[i]=false;
                list.remove(list.size()-1);
            }
        }
    }


    public static void main(String[] args) {
        int[] a=new int[]{1,2,3};
        System.out.println(new Solutions().permute(a).toString());
    }
}
